﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Bingosoft.Data;

namespace Bingosoft.Modules.Demo.User
{
    public class UserService
    {
        private readonly Dao _dao = Dao.Get();

        public String SaveOrUpdateUser(UserInfo user)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                if (string.IsNullOrEmpty(user.Id))
                {
                    user.Id = Guid.NewGuid().ToString();
                    this._dao.ExecuteNonQuery("demo.user.insert", user);
                }
                else
                {
                    this._dao.ExecuteNonQuery("demo.user.update", user);
                }
                transactionScope.Complete();
            }
            return user.Id;
        }

        public void DeleteUser(String userId)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "Id", userId } };
                this._dao.ExecuteNonQuery("demo.user.delete", param);
                transactionScope.Complete();
            }

        }

        public UserInfo GetUser(String userId)
        {
            var param = new Dictionary<string, object>() { { "Id", userId } };
            return this._dao.QueryEntity<UserInfo>("demo.user.get", param);
        }
    }
}
